package login;//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.*;
//import javax.servlet.http.HttpSession;
//import javax.swing.JOptionPane;
//import JavaBean.User;
//import java.sql.Connection;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.sql.Statement;
//
///**
// * @author Sai
// * Created by Sai on 2019-03-03.
// */
//@SuppressWarnings("Duplicates")
//@WebServlet(urlPatterns = {"/login.Regist"})
//public class login.Regist extends HttpServlet {
//
//    @Override
//
//    protected void service(HttpServletRequest request, HttpServletResponse response) throws  IOException {
////        int i;
////        try {
////            HttpSession session = request.getSession();
////            request.setCharacterEncoding("UTF-8");
////            String user = request.getParameter("username");
////            String sno = request.getParameter("stunumber");
////            String Password = request.getParameter("pa");
////            Connection conn = (Connection) getServletContext().getAttribute("connection");
////            Statement stmt = conn.createStatement();
////            ResultSet rs = stmt.executeQuery("select * from personMessage");
////            while(rs.next()){
////                if(rs.getString("user").equals(username))
////                    flag=1;
////            }
////            if(flag==1){
////                JOptionPane.showMessageDialog(null, "该用户已存在", " 请重新注册", JOptionPane.ERROR_MESSAGE);
////                response.sendRedirect("/register.jsp");
////            }
////            else{
////                User userr = new User( user, Email,Password,img,sno);
////                session.setAttribute("user",userr);
////                response.sendRedirect("UserPage.jsp");
////            }
//
////        }
////        catch(IOException e){
////            response.sendRedirect("/error.jsp");
////        }
//
//    }
//}
import JavaBean.User;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name="login.Regist",urlPatterns = {"/login/Regist"})
public class Regist extends HttpServlet {
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = (EntityManager)request.getAttribute("_entity.manager");

        try{
            String user = request.getParameter("username");
            String sno = request.getParameter("stunumber");
            String Password = request.getParameter("pa");
            String email=request.getParameter("email");
            String img= "picture/"+sno+".jpg";
            User userr = new User( user,email,Password,img,sno);
            em.persist(userr);

            request.getSession().setAttribute("user",userr);

            response.sendRedirect("/UserPage.jsp");
        }
        catch(Exception e){
            throw new ServletException(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    public String getServletInfo() {
        return "Short description";
    }
}